package net.hubt.itc.ui;

import net.hubt.itc.data.LookupCommand;
import net.hubt.itc.data.ScheduleData;
import net.hubt.itc.data.ScheduleXMLParser;
import net.hubt.itc.data.StudentScheduleList;

import net.hubt.itc.io.net.HttpUtils;
import net.hubt.itc.ui.utils.CTable;
import net.hubt.itc.util.LayoutUtils;
import net.hubt.itc.util.XMLDocument;
import android.app.AlertDialog;
import android.app.ProgressDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;

public class ScheduleActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStart() {
		super.onStart();
		executeGetScheduleTask();
	}

	private void executeGetScheduleTask() {
		prepareParamsForTask();
		new GetScheduleTask().execute(baseUrl + "/xml.php", "msv", userId,
				"type", LookupCommand.CMD_SCHEDULE + "");
		
	}

	class GetScheduleTask extends AsyncTask<String, String, String> {

		ProgressDialog dlgProgress;
		@Override
		protected void onPreExecute() {		
			super.onPreExecute();
			dlgProgress=ProgressDialog.show(ScheduleActivity.this, getString(R.string.app_global_load_title),
					getString(R.string.app_global_load_message),true);
		}
		@Override
		protected String doInBackground(String... addresses) {
			try {
				//connect to network, get xml data
				String scheduleXmlData = HttpUtils.urlContentPost(addresses);
				
				return scheduleXmlData;
			} catch (Exception ex) {
				//network error
				return "100";
			}
		}
		

		@Override
		protected void onProgressUpdate(String... values) {
		
		}

		@Override
		protected void onPostExecute(String scheduleXmlData) {
			// tvContent.setText(transcriptXmlData);
			// xmlContent=result;
			//network error
			if(scheduleXmlData.equalsIgnoreCase("100")){			
				dlgProgress.dismiss();
				AlertDialog dialog=new AlertDialog.Builder(ScheduleActivity.this).create();
				dialog.setTitle(getString(R.string.ui_schedule_desc_short));
				//dialog.setMessage(getString(R.string.ui_trans_error_msg));			
				dialog.setMessage(getString(R.string.ui_common_network_error_msg));
				dialog.setIcon(android.R.drawable.ic_dialog_info);
				dialog.setButton("OK", new DialogInterface.OnClickListener() {				
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}); 
					
				dialog.show();
			}			
			else{ // get xml data -> process and display
				processScheduleData(scheduleXmlData);
	//			Log.e("Xml", scheduleXmlData);
				dlgProgress.dismiss();
			}
		}
	}
	

	protected void processScheduleData(String xmlData) {
		XMLDocument xmlDoc = new XMLDocument();
		//Document document = xmlDoc.getDocument(xmlData);

		ScheduleXMLParser sp = new ScheduleXMLParser();
		sp.setRoot(xmlDoc.getRootNode(xmlData));

		StudentScheduleList studentSchedule = sp.parseXmlData();
		if(studentSchedule!=null){
			CTable scheduleTableUI = buildScheduleTableUI(this,
					studentSchedule);
			CTable scheduleTableHeaderUI=buildScheduleTableHeaderUI(this, studentSchedule);
			
//			setContentView(LayoutUtils.createLinearScrollableTableLayout(this,
//					scheduleTableUI,
//					getString(R.string.ui_schedule_desc)
//							+ studentSchedule.getTenSinhVien()));
			setContentView(LayoutUtils.createLinearScrollableTableLayout(this,
					scheduleTableUI,scheduleTableHeaderUI));
		}
		else{ // can connect to server, but no data found
			AlertDialog dialog=new AlertDialog.Builder(ScheduleActivity.this).create();
			dialog.setTitle(getString(R.string.ui_schedule_desc_short));
			//dialog.setMessage(getString(R.string.ui_trans_error_msg));			
			dialog.setMessage(getString(R.string.ui_schedule_not_found_msg));
			dialog.setIcon(android.R.drawable.ic_dialog_info);
			dialog.setButton("OK", new DialogInterface.OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					startActivity(new Intent(ScheduleActivity.this, MainUI.class));
				}
			}); 
				
			dialog.show();					
		}

	}
	
	
	protected CTable buildScheduleTableHeaderUI(Context context,StudentScheduleList studentScheduleList){
		CTable scheduleHeaderTbl=new CTable(context,android.widget.TableLayout.LayoutParams.FILL_PARENT,
				android.widget.TableLayout.LayoutParams.WRAP_CONTENT);
		scheduleHeaderTbl.stretchColumn(0, true);
		scheduleHeaderTbl.stretchColumn(1, true);
		if(studentScheduleList!=null){
			//the headers colors can be changed to Color.DKGRAY
			scheduleHeaderTbl.addRow(scheduleHeaderTbl.makeRow(new int[] {
					Gravity.RIGHT, Gravity.RIGHT },Color.BLACK,Typeface.DEFAULT_BOLD, new boolean[]{false,true}, getString(R.string.ui_schedule_hoten),
					studentScheduleList.getTenSinhVien()));
			scheduleHeaderTbl.addRow(scheduleHeaderTbl.makeRow(new int[] {
					Gravity.RIGHT, Gravity.RIGHT }, Color.BLACK,Typeface.DEFAULT_BOLD, new boolean[]{false,true},getString(R.string.ui_schedule_msv),
					studentScheduleList.getMaSv()));
			scheduleHeaderTbl.addRow(scheduleHeaderTbl.makeRow(new int[] {
					Gravity.RIGHT, Gravity.RIGHT }, Color.BLACK,Typeface.DEFAULT_BOLD, new boolean[]{false,true},getString(R.string.ui_schedule_lop),
					studentScheduleList.getLop()));
		}
		return scheduleHeaderTbl;
	}
	protected CTable buildScheduleTableUI(Context context,
			StudentScheduleList studentSchedule) {
		CTable scheduleTable = new CTable(context);
		scheduleTable.stretchColumn(0, true);
		scheduleTable.stretchColumn(1, true);
		if (studentSchedule != null) {
			int cnt = studentSchedule.size();
			for (int i = 0; i < cnt; i++) {
				ScheduleData scheduleObject = studentSchedule
						.get(i);			
				
				scheduleTable.addRow(scheduleTable.makeColSpanRow(new int[]{Gravity.RIGHT},2, new boolean[]{true},scheduleObject.getNgayThi()));
				scheduleTable.addRow(scheduleTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_schedule_monthi),
						scheduleObject.getMonThi()));
				scheduleTable.addRow(scheduleTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_schedule_phongthi),
						scheduleObject.getPhongThi()));
				scheduleTable.addRow(scheduleTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_schedule_giothi),
						scheduleObject.getGioThi() + ":"+scheduleObject.getPhutThi()));
				/* ghep gio thi va phut thi thanh thoi gian thi
				scheduleTable.addRow(scheduleTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_schedule_phutthi),
						scheduleObject.getPhutThi()));
						*/
				scheduleTable.addRow(scheduleTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_schedule_kiemtra),
						scheduleObject.getKiemTra()));
				scheduleTable.addRow(scheduleTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_schedule_diemtru),
						scheduleObject.getDiemTru()));
				
				
			}
		}		
		return scheduleTable;
	}
}
